package com.faxsun.profile.core.domain;

import com.faxsun.core.tool.MobilePhoneMask;
import org.broadleafcommerce.common.presentation.AdminPresentation;
import org.broadleafcommerce.common.presentation.AdminPresentationCollection;
import org.broadleafcommerce.common.presentation.client.AddMethodType;
import org.broadleafcommerce.profile.core.domain.CustomerImpl;
import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Cascade;
import org.springframework.util.StringUtils;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "FS_BLC_CUSTOMER")
public class FSCustomerImpl extends CustomerImpl implements FSCustomer {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    @Column(name = "CHANNEL_ID_3TH")
    protected String channelId;

    @Column(name = "IS_3TH_CUSTOMER", nullable = false)
    @AdminPresentation(friendlyName = "FSCustomerImpl_CUSTOMER3TH", order = 1000, tab = Presentation.Tab.Name.Contact,
        tabOrder = Presentation.Tab.Order.Contact)
    protected boolean isCustomer3th = false;

    @SuppressWarnings("deprecation")
    @OneToMany(mappedBy = "customer", targetEntity = FSReceiverImpl.class, cascade = {CascadeType.ALL})
    @Cascade(value = {org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "blStandardElements")
    @AdminPresentationCollection(friendlyName = "FSReceiverImpl_IDCard", order = 1000, addType = AddMethodType
        .PERSIST, tab = Presentation.Tab.Name.Contact, tabOrder = Presentation.Tab.Order.Contact)
    protected List<FSReceiver> customerIDCards = new ArrayList<FSReceiver>();

    @Column(name = "MOBILE", unique = true)
    @AdminPresentation(friendlyName = "FSCustomerImpl_Mobile", order = 1000, tab = Presentation.Tab.Name.Contact,
        tabOrder = Presentation.Tab.Order.Contact)
    protected String mobile;

    @Column(name = "IS_VERIFIED", nullable = false)
    @AdminPresentation(friendlyName = "FSCustomerImpl_IsVerfified", order = 1000, tab = Presentation.Tab.Name.Contact,
        tabOrder = Presentation.Tab.Order.Contact)
    protected boolean isVerified = false;

    //default = 0.
    @Column(name = "IS_MERGED", columnDefinition = "bit(1) default 0", nullable = false)
    @AdminPresentation(friendlyName = "FSCustomerImpl_IsMerged", order = 1000, tab = Presentation.Tab.Name.Contact,
        tabOrder = Presentation.Tab.Order.Contact)
    protected boolean isMerged = false;

    @Column(name = "REFERRAL_CODE", unique = true)
    @AdminPresentation(friendlyName = "FSCustomerImpl_ReferralCode", tab = Presentation.Tab.Name.Advanced,
        tabOrder = Presentation.Tab.Order.Advanced)
    protected String referralCode;

    @OneToMany(targetEntity = FSReferralCustomerOrderItemXrefImpl.class, mappedBy = "customer")
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region = "blStandardElements")
    @AdminPresentationCollection(friendlyName = "FSCustomerImpl_Referral_OrderItems", addType = AddMethodType.PERSIST,
        tab = Presentation.Tab.Name.Advanced, tabOrder = Presentation.Tab.Order.Advanced)
    @BatchSize(size = 50)
    protected List<FSReferralCustomerOrderItemXref> referralCustomerOrderItemXrefs;

    @Override
    public String getFirstName() {
        if (firstName == null || firstName.isEmpty()) {
            return "";
        }
        return MobilePhoneMask.maskMobilePhone(firstName);
    }

    public List<FSReceiver> getCustomerIDCards() {
        return customerIDCards;
    }

    public void setCustomerIDCards(List<FSReceiver> customerIDCards) {
        this.customerIDCards = customerIDCards;
    }

    public String getChannelId() {
        return channelId;
    }

    public void setChannelId(String channelId) {
        this.channelId = channelId;
    }

    public void setIsCustomer3th(boolean is3th) {
        this.isCustomer3th = is3th;
    }

    public boolean isCustomer3th() {
        return isCustomer3th;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public boolean isVerified() {
        return isVerified;
    }

    public void setVerified(boolean isVerified) {
        this.isVerified = isVerified;
    }

    public boolean isMerged() {
        return isMerged;
    }

    public void setMerged(boolean merged) {
        this.isMerged = merged;
    }

    public String getReferralCode() {
        return referralCode;
    }

    public void setReferralCode(String referralCode) {
        this.referralCode = referralCode;
    }

    public List<FSReferralCustomerOrderItemXref> getReferralCustomerOrderItemXrefs() {
        return referralCustomerOrderItemXrefs;
    }

    public void setReferralCustomerOrderItemXrefs(List<FSReferralCustomerOrderItemXref> referralCustomerOrderItemXrefs) {
        this.referralCustomerOrderItemXrefs = referralCustomerOrderItemXrefs;
    }

	@Override
	public boolean isBindedToEmail() {
		return !StringUtils.isEmpty(this.emailAddress);
	}

}
